package com.hsck.order.kds.base.isnoam.view.keyboard

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.hsck.order.kds.R

/**
 * @author isnoam
 * @description：九宫格键盘适配器
 */

class KeyBoardAdapter(
    private val mContext: Context,
    private val valueList: ArrayList<Map<String, String>>,
) :
    BaseAdapter() {
    override fun getCount(): Int {
        return valueList.size
    }

    override fun getItem(position: Int): Any {
        return valueList[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView
        val viewHolder: ViewHolder
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.grid_item_virtual_keyboard, null)
            viewHolder = ViewHolder()
            viewHolder.btnKey = convertView.findViewById<View>(R.id.btn_keys) as TextView
            viewHolder.tvDelete = convertView.findViewById<View>(R.id.tv_delete) as TextView
            viewHolder.ivDelete = convertView.findViewById<View>(R.id.iv_delete) as ImageView
            convertView.tag = viewHolder
        } else {
            viewHolder = convertView.tag as ViewHolder
        }
        when (position) {
            9 -> {
                viewHolder.btnKey!!.text = valueList[position]["name"]
                viewHolder.tvDelete!!.visibility = View.INVISIBLE
                viewHolder.ivDelete!!.visibility = View.INVISIBLE
                viewHolder.btnKey!!.visibility = View.VISIBLE
            }

            11 -> {
                viewHolder.tvDelete!!.visibility = View.VISIBLE
                viewHolder.ivDelete!!.visibility = View.VISIBLE
                viewHolder.btnKey!!.visibility = View.INVISIBLE
            }

            else -> {
                viewHolder.tvDelete!!.visibility = View.INVISIBLE
                viewHolder.ivDelete!!.visibility = View.INVISIBLE
                viewHolder.btnKey!!.visibility = View.VISIBLE
                viewHolder.btnKey!!.text = valueList[position]["name"]
            }
        }
        return convertView!!
    }

    /**
     * 存放控件
     */
    inner class ViewHolder {
        var btnKey: TextView? = null
        var tvDelete: TextView? = null
        var ivDelete: ImageView? = null
    }
}

